package org.ims.inv.service.flow.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.ims.inv.dao.flow.DAOFlowTemplate;
import org.ims.inv.model.flow.ModelFlowTemplate;
import org.ims.inv.service.flow.ServiceFlowTemplate;

import cn.trymore.core.exception.DAOException;
import cn.trymore.core.exception.ServiceException;
import cn.trymore.core.service.impl.ServiceGenericImpl;
import cn.trymore.core.util.UtilCollection;
import cn.trymore.core.util.UtilString;

/**
 * Service: 工作流模板
 * 
 * @author Jeccy.Zhao
 *
 */
public class ServiceFlowTemplateImpl
extends ServiceGenericImpl<ModelFlowTemplate> implements ServiceFlowTemplate
{
	
	/**
	 * Repository of flow template
	 */
	private DAOFlowTemplate daoFlowTemplate;
	
	public ServiceFlowTemplateImpl(DAOFlowTemplate dao)
	{
		super(dao);
		
		this.daoFlowTemplate = dao;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.flow.ServiceFlowTemplate#getBySequence(java.lang.Integer)
	 */
	@Override
	public ModelFlowTemplate getBySequence(Integer sequence, ModelFlowTemplate domain)
			throws ServiceException
	{
		try
		{			
			ModelFlowTemplate entity = new ModelFlowTemplate();
			entity.setSequence(sequence);
			entity.setFlowDef(domain.getFlowDef());
			
			List<ModelFlowTemplate> result = this.daoFlowTemplate.getListByCriteria(this.getCriterias(entity));
			if (UtilCollection.isNotEmpty(result) && result.size() == 1)
			{
				return result.get(0);
			}
			else
			{
				throw new ServiceException("More than one flow teamplate row data found by sequence: " + sequence);
			}
		}
		catch (DAOException e)
		{
			throw new ServiceException(e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see cn.trymore.core.service.impl.ServiceGenericImpl#getCriterias(cn.trymore.core.model.ModelBase)
	 */
	@Override
	protected DetachedCriteria getCriterias(ModelFlowTemplate domain)
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelFlowTemplate.class);
		
		if (domain != null)
		{
			if (domain.getSequence() != null && domain.getSequence() > 0)
			{
				criteria.add(Restrictions.eq("sequence", domain.getSequence()));
			}
			
			if (UtilString.isNotEmpty(domain.getNodeTitle()))
			{
				criteria.add(Restrictions.eq("nodeTitle", domain.getNodeTitle()));
			}
			
			if (domain.getFlowDef() != null && UtilString.isNotEmpty(domain.getFlowDef().getId()))
			{
				criteria.createCriteria("flowDef").add(Restrictions.eq("id", domain.getFlowDef().getId()));
			}
			
			if (domain.getDependedFlow() != null && UtilString.isNotEmpty(domain.getDependedFlow().getId()))
			{
				criteria.createCriteria("dependedFlow").add(Restrictions.eq("id", domain.getDependedFlow().getId()));
			}
			
		}
		
		return criteria;
	}
	
	public void setDaoFlowTemplate(DAOFlowTemplate daoFlowTemplate)
	{
		this.daoFlowTemplate = daoFlowTemplate;
	}

	public DAOFlowTemplate getDaoFlowTemplate()
	{
		return daoFlowTemplate;
	}

}
